<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>更改Ubuntu apt源 </title>
    <script>
        const VERSION_LIST = [{
            "code": "23.10",
            "name": "Mantic Minotaur",
        },
        {
            "code": "23.04",
            "name": "Lunar Lobster",
        },
        {
            "code": "22.10",
            "name": "Kinetic Kudu",
        },
        {
            "code": "22.04 LTS",
            "name": "Jammy Jellyfish",
            "default": true,
        },
        {
            "code": "21.10",
            "name": "Impish Indri",
        },
        {
            "code": "21.04",
            "name": "Hirsute Hippo",
        },
        {
            "code": "20.10",
            "name": "Groovy Gorilla",
        },
        {
            "code": "20.04 LTS",
            "name": "Focal Fossa",
        },
        {
            "code": "19.10",
            "name": "Eoan Ermine",
        },
        {
            "code": "19.04",
            "name": "Disco Dingo",
        },
        {
            "code": "18.10",
            "name": "Cosmic Cuttlefish",
        },
        {
            "code": "18.04 LTS",
            "name": "Bionic Beaver",
        },
        {
            "code": "17.10",
            "name": "Artful Aardvark",
        },
        {
            "code": "17.04",
            "name": "Zesty Zapus",
        },
        {
            "code": "16.10",
            "name": "Yakkety Yak",
        },
        {
            "code": "16.04 LTS",
            "name": "Xenial Xerus",
        },
        {
            "code": "15.10",
            "name": "Wily Werewolf",
        },
        {
            "code": "15.04",
            "name": "Vivid Vervet",
        },
        {
            "code": "14.10",
            "name": "Utopic Unicorn",
        },
        {
            "code": "14.04 LTS",
            "name": "Trusty Tahr",
        },
        {
            "code": "13.10",
            "name": "Saucy Salamander",
        },
        {
            "code": "13.04",
            "name": "Raring Ringtail",
        },
        {
            "code": "12.10",
            "name": "Quantal Quetzal",
        },
        {
            "code": "12.04 LTS",
            "name": "Precise Pangolin",
        },
        {
            "code": "11.10",
            "name": "Oneiric Ocelot",
        },
        {
            "code": "11.04",
            "name": "Natty Narwhal",
        },
        {
            "code": "10.10",
            "name": "Maverick Meerkat",
        },
        {
            "code": "10.04 LTS",
            "name": "Lucid Lynx",
        },
        {
            "code": "9.10",
            "name": "Karmic Koala",
        },
        {
            "code": "9.04",
            "name": "Jaunty Jackalope",
        },
        {
            "code": "8.10",
            "name": "Intrepid Ibex",
        },
        {
            "code": "8.04 LTS",
            "name": "Hardy Heron",
        },
        {
            "code": "7.10",
            "name": "Gutsy Gibbon",
        },
        {
            "code": "7.04",
            "name": "Feisty Fawn",
        },
        {
            "code": "6.10",
            "name": "Edgy Eft",
        },
        {
            "code": "6.06 LTS",
            "name": "Dapper Drake",
        },
        {
            "code": "5.10",
            "name": "Breezy Badger",
        },
        {
            "code": "5.04",
            "name": "Hoary Hedgehog",
        },
        {
            "code": "4.10",
            "name": "Warty Warthog",
        }]
        const SOURCE_TEMPLATE = "# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to\n\
# newer versions of the distribution.\n\
deb {mirror} {version} main restricted\n\
{disable-source}deb-src {mirror} {version} main restricted\n\
\n\
## Major bug fix updates produced after the final release of the\n\
## distribution.\n\
deb {mirror} {version}-updates main restricted\n\
{disable-source}deb-src {mirror} {version}-updates main restricted\n\
\n\
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu\n\
## team. Also, please note that software in universe WILL NOT receive any\n\
## review or updates from the Ubuntu security team.\n\
deb {mirror} {version} universe\n\
{disable-source}deb-src {mirror} {version} universe\n\
deb {mirror} {version}-updates universe\n\
{disable-source}deb-src {mirror} {version}-updates universe\n\
\n\
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu\n\
## team, and may not be under a free licence. Please satisfy yourself as to\n\
## your rights to use the software. Also, please note that software in\n\
## multiverse WILL NOT receive any review or updates from the Ubuntu\n\
## security team.\n\
deb {mirror} {version} multiverse\n\
{disable-source}deb-src {mirror} {version} multiverse\n\
deb {mirror} {version}-updates multiverse\n\
{disable-source}deb-src {mirror} {version}-updates multiverse\n\
\n\
## N.B. software from this repository may not have been tested as\n\
## extensively as that contained in the main release, although it includes\n\
## newer versions of some applications which may provide useful features.\n\
## Also, please note that software in backports WILL NOT receive any review\n\
## or updates from the Ubuntu security team.\n\
deb {mirror} {version}-backports main restricted universe multiverse\n\
{disable-source}deb-src {mirror} {version}-backports main restricted universe multiverse\n\
\n\
deb {security-mirror} {version}-security main restricted\n\
{disable-source}deb-src {security-mirror} {version}-security main restricted\n\
deb {security-mirror} {version}-security universe\n\
{disable-source}deb-src {security-mirror} {version}-security universe\n\
deb {security-mirror} {version}-security multiverse\n\
{disable-source}deb-src {security-mirror} {version}-security multiverse"
        const MIRROR_LIST = [
            {
                "name": "官方源",
                "mirror": "archive.ubuntu.com/ubuntu/ ",
                "security-mirror": "security.ubuntu.com/ubuntu/",
                "https": false,
            },
            {
                "name": "华为开源镜像站",
                "mirror": "repo.huaweicloud.com/ubuntu/",
                "security-mirror": "repo.huaweicloud.com/ubuntu/",
                "help": "https://mirrors.huaweicloud.com/home",
                "https": true,
            },
            {
                "name": "阿里巴巴开源镜像站",
                "mirror": "mirrors.aliyun.com/ubuntu/",
                "security-mirror": "mirrors.aliyun.com/ubuntu/",
                "help": "https://developer.aliyun.com/mirror/ubuntu",
                "https": true,
                "default": true,
            },
            {
                "name": "腾讯软件源",
                "mirror": "mirrors.cloud.tencent.com/ubuntu/",
                "security-mirror": "mirrors.cloud.tencent.com/ubuntu/",
                "help": "https://mirrors.cloud.tencent.com/help/ubuntu.html",
                "https": true,
            },
            {
                "name": "网易开源镜像站",
                "mirror": "mirrors.163.com/ubuntu/",
                "security-mirror": "mirrors.163.com/ubuntu/",
                "help": "https://mirrors.163.com/.help/ubuntu.html",
                "https": true,
            },
            {
                "name": "浙江大学开源镜像站",
                "mirror": "mirrors.zju.edu.cn/ubuntu/",
                "security-mirror": "mirrors.zju.edu.cn/ubuntu/",
                "help": "https://mirrors.zju.edu.cn/docs/ubuntu/",
                "https": true,
            },
            {
                "name": "上海交通大学软件源镜像服务",
                "mirror": "mirror.sjtu.edu.cn/ubuntu/",
                "security-mirror": "mirror.sjtu.edu.cn/ubuntu/",
                "help": "https://mirror.sjtu.edu.cn/docs/ubuntu",
                "https": true,
            },
            {
                "name": "大连东软信息学院",
                "mirror": "mirrors.neusoft.edu.cn/ubuntu/",
                "security-mirror": "mirrors.neusoft.edu.cn/ubuntu/",
                "https": true,
            },
            {
                "name": "清华大学开源软件镜像站",
                "mirror": "mirrors.tuna.tsinghua.edu.cn/ubuntu/",
                "security-mirror": "mirrors.tuna.tsinghua.edu.cn/ubuntu/",
                "help": "https://mirrors.tuna.tsinghua.edu.cn/help/ubuntu/",
                "https": true,
            },
            {
                "name": "北京大学开源镜像站",
                "mirror": "mirrors.pku.edu.cn/ubuntu/",
                "security-mirror": "mirrors.pku.edu.cn/ubuntu/",
                "help": "https://mirrors.pku.edu.cn/Help/Ubuntu",
                "https": true,
            },
            {
                "name": "重庆大学开源软件镜像站",
                "mirror": "mirrors.cqu.edu.cn/ubuntu/",
                "security-mirror": "mirrors.cqu.edu.cn/ubuntu/",
                "help": "https://mirrors.cqu.edu.cn/#/wiki/mirror-wiki/ubuntu",
                "https": true,
            }]
    </script>
    <style>
        html,
        body {
            width: 100%;
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        #app {
            padding: 0 16px;
            box-sizing: border-box;
            max-width: 1080px;
            margin: auto;
        }

        h1 {
            padding-left: 16px;
            box-sizing: border-box;
            max-width: 1080px;
            margin: 0.67em auto;
        }

        #app>div {
            padding: 4px;
        }

        .apt-source {
            margin: 8px 0;
            padding: 8px !important;
            resize: none;
            width: 100%;
            box-sizing: border-box;
            background: #eee;
            border: solid 1px #999;
            border-radius: 4px;
            font-size: 13px;
            height: 480px;
            overflow: auto;
        }

        a {
            color: dodgerblue;
            font-weight: bold;
            text-decoration: none;
            font-size: 0.96em;
        }
    </style>
</head>

<body>
    <h1>Ubuntu 软件源生成器</h1>
    <div id="app">
        <div class="msg">欢迎使用本生成器，在使用之前，请确保您需要修改软件源的操作系统为Ubuntu，处理器为32/64位x86架构，本生成器不适用于Debian等其他使用apt作为管理器的其他操作系统。
        </div>
        <div>
            启用 HTTPS <select id="enable-https">
                <option value="1" selected>是【推荐】</option>
                <option value="0">否</option>
            </select>
        </div>
        <div>
            使用官方安全更新软件源 <select id="enable-offical-security">
                <option value="0" selected>否</option>
                <option value="1">是</option>
            </select>
        </div>
        <div>
            使用源码镜像 <select id="enable-src">
                <option value="0" selected>否【推荐】</option>
                <option value="1">是</option>
            </select>
        </div>
        <div>
            请选择您正在使用的操作系统版本 <select id="system-version"></select>
        </div>
        <div>
            请选择您希望使用的镜像站 <select id="mirror-station"></select>
        </div>
        <div class="help">如需使用内网镜像源或其他需求请 <a href="#" target="_blank">查看帮助</a></div>
        <div>
            <button id="copy-list">复制</button>
            <button id="download-list">下载</button>
        </div>
        <textarea class="apt-source"></textarea>
    </div>
    <script>
        const apt_source = document.querySelector(".apt-source");
        const ver_select = document.querySelector("#system-version");
        const mir_select = document.querySelector("#mirror-station");
        const enable_https = document.querySelector("#enable-https");
        const enable_src = document.querySelector("#enable-src");
        const offical_sec = document.querySelector("#enable-offical-security");
        const help_text = document.querySelector(".help");

        VERSION_LIST.forEach(e => {
            const opt = document.createElement("option");
            opt.textContent = e.code;
            opt.value = e.name;
            if (e.default) opt.selected = true;
            ver_select.append(opt);
        });
        MIRROR_LIST.forEach((e, i) => {
            const opt = document.createElement("option");
            opt.textContent = e.name;
            opt.value = i.toString();
            if (e.default) opt.selected = true;
            mir_select.append(opt);
        });

        function formatAptSource(mirror = MIRROR_LIST[0], version = "Jammy Jellyfish", https = true, off_sec = false, src = false) {
            let source = SOURCE_TEMPLATE;
            const version_name = version.split(" ")[0].toLowerCase();

            source = source.replace(/\{mirror\}/g, (https && mirror.https ? "https://" : "http://") + mirror.mirror);
            if (off_sec) {
                source = source.replace(/\{security-mirror\}/g, "http://" + MIRROR_LIST[0]["security-mirror"]);
            } else {
                source = source.replace(/\{security-mirror\}/g, (https && mirror.https ? "https://" : "http://") + mirror["security-mirror"]);
            }
            source = source.replace(/\{disable-source\}/g, src ? "" : "# ");
            source = source.replace(/\{version\}/g, version_name);
            return source;
        }

        function changeMirror() {
            const mirror = MIRROR_LIST[parseInt(mir_select.value)];
            if (mirror.https === false) {
                enable_https.value = "0";
                enable_https.value = "0";
                enable_https.disabled = true;
            } else if (enable_https.disabled) {
                enable_https.value = "1";
                enable_https.disabled = false;
            }
            if (mir_select.value === "0") {
                offical_sec.value = "1";
                offical_sec.disabled = true;
            } else if (offical_sec.disabled) {
                offical_sec.value = "0";
                offical_sec.disabled = false;
            }
            if (mirror.help) {
                help_text.style.display = "";
                help_text.querySelector("a").href = mirror.help;
            } else {
                help_text.style.display = "none";
                help_text.querySelector("a").href = "#";
            }
            updateAptSource();
        }

        function updateAptSource(getSrc = false) {
            const mirror = MIRROR_LIST[parseInt(mir_select.value)];
            const https = enable_https.value === "1" ? true : false;
            const version_name = ver_select.value;
            const src = enable_src.value === "1" ? true : false;
            const offical = offical_sec.value === "1" ? true : false;
            const source = formatAptSource(mirror, version_name, https, offical, src);
            apt_source.value = source;
            if (getSrc) {
                return source;
            }
        }

        function coptToClipboard() {
            updateAptSource();
            apt_source.select();
            document.execCommand("copy");
        }

        function downloadSourceList() {
            updateAptSource(false);
            const src = updateAptSource(true);
            const blob = new Blob([src], {
                type: "texy/plain;charset=utf-8"
            });
            const obj_url = URL.createObjectURL(blob);
            const dl_el = document.createElement("a");
            dl_el.href = obj_url;
            dl_el.download = "source.list";
            dl_el.click();
            URL.revokeObjectURL(obj_url);
            dl_el.remove();
        }

        updateAptSource();

        document.querySelectorAll("select").forEach(e => {
            if (e.id === "mirror-station") e.addEventListener("change", changeMirror);
            else e.addEventListener("change", updateAptSource);
        });

        document.querySelector("#copy-list").addEventListener("click", coptToClipboard);
        document.querySelector("#download-list").addEventListener("click", downloadSourceList);
    </script>
</body>

</html>